<?php

namespace backend\modules\wechat\controllers;

use Yii;
use backend\controllers\DashboardController;

/**
 * click 点击事件 key 可自定义事件
 * view url地址
 * scancode_push 扫码事件 调用微信的扫码接口
 * pic_sysphoto 拍照
 * pic_photo_or_album拍照或者相册
 * pic_weixin 微信相册
 * location_select 选择地理位置
 * location_select 素材id
 * view_limited 永久素材ID
 * miniprogram 小程序
 *
 * Class MenuController
 * @package backend\modules\wechat\controllers
 */
class MenuController extends DashboardController
{
    public $layout = "@backend/views/layouts/wechat";
    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
        $appId = 1;
        $buttons = json_decode('[
            {
                "id": 1,
                "pid": 0,
                "type": "click",
                "name": "今日歌曲",
                "key": "V1001_TODAY_MUSIC",
                "content": "",
                "sub_button": []
            },
            {
                "id": 2,
                "pid": 0,
                "type": "click",
                "name": "歌手简介",
                "key": "V1001_TODAY_SINGER",
                "content": "",
                "sub_button": []
            },
            {
                "id": 4,
                "name": "菜单",
                "type": "none",
                "sub_button": [
                    {
                        "id": 5,
                        "pid": 4,
                        "type": "view",
                        "name": "搜索",
                        "url": "https://www.fankers.cn/",
                        "content": "",
                        "sub_button": []
                    },
                    {
                        "id": 6,
                        "pid": 4,
                        "type": "view",
                        "name": "视频",
                        "url": "https://www.baidu.cn/",
                        "content": "",
                        "sub_button": []
                    },
                    {
                        "id": 7,
                        "pid": 4,
                        "type": "click",
                        "name": "赞一下我们",
                        "key": "V1001_GOOD",
                        "sub_button": []
                    }
                ]
            }
        ]',JSON_UNESCAPED_UNICODE);


        return $this->render('index',compact('appId','buttons'));
    }

    public function actionSave()
    {
        $post = Yii::$app->request->post();
        $appId = $post['appId'];

        if (!$appId || empty($post['button']))Yii::$app->message->fail("参数错误");

        $button = $this->format4Wx($post['button']);

        $result = Yii::$app->wechat(1)->menu->create($button);

        var_dump($result);die();
        
    }

    private function format4Wx($data){
        $button = [];
        foreach($data as $k=>$v){
            if(!empty($v['sub_button'])){
                $sub = [];
                foreach($v['sub_button'] as $kk=>$vv){
                    $sub[] = array_filter($vv, function($key){
                        return in_array($key, ['name','type','url','key']);
                    },ARRAY_FILTER_USE_KEY);
                }
                $v['sub_button'] = $sub;
                unset($v['type']);
            }
            $button[] = array_filter($v, function($key){
                return in_array($key, ['name','type','url','key','sub_button']);
            },ARRAY_FILTER_USE_KEY);
        }
        return $button;
    }



    public function actionTest()
    {
        $menuInfo = Yii::$app->wechat(1)->menu->list();
        var_dump($menuInfo);die();
    }
}
